home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / vbcc.lha / vbcc / declaration.c < prev    next >
C/C++ Source or Header  |  1998-02-17  |  46KB  |  1,476 lines

  1. /*  $VER: vbcc (declaration.c) V0.4     */
  2.  
  3. #include "vbc.h"
  4.  
  5. static char FILE_[]=__FILE__;
  6.  
  7. #define PARAMETER 8
  8. #define OLDSTYLE 16
  9.  
  10. struct const_list *initialization(struct Typ *,int,int);
  11. int test_assignment(struct Typ *,np);
  12. int return_sc,return_reg,has_return;
  13.  
  14. extern int float_used;
  15. extern void optimize(long,struct Var *);
  16.  
  17. int settyp(int typnew, int typold)
  18. /* Unterroutine fuer declaration_specifiers().              */
  19. {
  20.   static int warned_long_double;
  21.   if(DEBUG&2) printf("settyp: new=%d old=%d\n",typnew,typold);
  22.   if(typold==LONG&&typnew==FLOAT){ error(203); return(DOUBLE);}
  23.   if(typold==LONG&&typnew==DOUBLE){
  24.     if(!warned_long_double){error(204);warned_long_double=1;}
  25.     return(DOUBLE);
  26.   }
  27.   if(typold!=0&&typnew!=INT){error(47);return(typnew);}
  28.   if(typold==0&&typnew==INT) return(INT);
  29.   if(typold==0) return(typnew);
  30.   if(typold==SHORT||typold==LONG) return(typold);
  31.   error(48);
  32.   return(typnew);
  33. }
  34.  
  35. #define dsc if(storage_class) error(49); if(typ||type_qualifiers) error(50)
  36. #define XSIGNED 16384
  37.  
  38. struct Typ *declaration_specifiers(void)
  39. /* Erzeugt neuen Typ und gibt Zeiger darauf zurueck,      */
  40. /* parst z.B. unsigned int, struct bla etc.               */
  41. {
  42.   int typ=0,type_qualifiers=0,notdone,storage_class,hard_reg;
  43.   char *merk,*imerk,sident[MAXI],sbuff[MAXI];
  44.   struct Typ *new=mymalloc(TYPS),*t,*ts;
  45.   struct struct_declaration *ssd;
  46.   struct struct_list (*sl)[];
  47.   size_t slsz;
  48.   struct Var *v;
  49.   storage_class=hard_reg=0;
  50.   new->next=0; new->exact=0;
  51.   do{
  52.     killsp();merk=s;cpbez(buff,0);notdone=0;
  53.     if(DEBUG&2) printf("ts: %s\n",buff);
  54.     if(!strcmp("struct",buff)) notdone=STRUCT;
  55.     if(!strcmp("union",buff)) notdone=UNION;
  56.     if(notdone!=0){
  57.       killsp();
  58.       if(*s!='{'){
  59.     cpbez(sident,1);
  60.     killsp();
  61.     ssd=find_struct(sident,0);
  62.     if(ssd&&*s=='{'&&find_struct(sident,nesting)&&ssd->count>0) error(13,sident);
  63.     if(!ssd||((*s=='{'||*s==';')&&!find_struct(sident,nesting))){
  64.       typ=settyp(notdone,typ);
  65.       ssd=mymalloc(sizeof(*ssd));
  66.       ssd->count=0;
  67.       new->exact=ssd=add_sd(ssd);
  68.       add_struct_identifier(sident,ssd);
  69.     }else{
  70.       new->exact=ssd;
  71.       typ=settyp(new->flags=notdone,typ);
  72.     }
  73.       }else{
  74.     *sident=0;
  75.     typ=settyp(notdone,typ);
  76.     ssd=mymalloc(sizeof(*ssd));
  77.     ssd->count=0;
  78.     new->exact=ssd=add_sd(ssd);
  79.       }
  80.       if(*s=='{'){
  81.     s++;
  82.     killsp();
  83.     slsz=SLSIZE;
  84.     sl=mymalloc(slsz*sizeof(struct struct_list));
  85.     ssd->count=0;
  86.     imerk=ident;
  87.     ts=declaration_specifiers();
  88.     while(*s!='}'&&ts){
  89.       ident=sbuff;
  90.       t=declarator(clone_typ(ts));
  91.       killsp();
  92.       if(*s==':'){
  93.         /*  bitfields werden hier noch ignoriert    */
  94.         np tree;
  95.         if((ts->flags&NQ)!=INT) error(51);
  96.         s++;killsp();tree=assignment_expression();
  97.         if(type_expression(tree)){
  98.           if(tree->flags!=CEXPR) error(52);
  99.           if((tree->ntyp->flags&NQ)<CHAR||(tree->ntyp->flags&NQ)>LONG) error(52);
  100.         }
  101.         if(tree) free_expression(tree);
  102.       }else{
  103.         if(*ident==0) error(53);
  104.       }
  105.       if(type_uncomplete(t)){
  106.         error(14,sbuff);
  107.         freetyp(t);
  108.         break;
  109.       }
  110.       if((t->flags&NQ)==FUNKT)
  111.         error(15,sbuff);
  112.       
  113.       if(*ident!=0){
  114.         int i=ssd->count;
  115.         while(--i>=0)
  116.           if(!strcmp((*sl)[i].identifier,ident))
  117.         error(16,ident);
  118.       }
  119.       (*sl)[ssd->count].styp=t;
  120.       (*sl)[ssd->count].identifier=add_identifier(ident,strlen(ident));
  121.       ssd->count++;
  122.       if(ssd->count>=slsz-1){
  123.         slsz+=SLSIZE;
  124.         sl=realloc(sl,slsz*sizeof(struct struct_list));
  125.         if(!sl){error(12);raus();}
  126.       }
  127.       killsp();
  128.       if(*s==',') {s++;killsp();continue;}
  129.       if(*s!=';') error(54); else s++;
  130.       killsp();
  131.       if(*s!='}'){
  132.         if(ts) freetyp(ts);
  133.         ts=declaration_specifiers();killsp();
  134.       }
  135.     }
  136.     if(ts) freetyp(ts);
  137.     if(ssd->count==0) error(55);
  138.     ident=imerk;
  139.     add_sl(ssd,sl);
  140.     free(sl);
  141.     if(*s!='}') error(56); else s++;
  142.     new->flags=notdone|type_qualifiers;
  143.       }
  144.       notdone=1;
  145.     }
  146.     if(!strcmp("enum",buff)){
  147.       /*  enumerations; die Namen werden leider noch ignoriert    */
  148.       killsp();notdone=1;
  149.       if(*s!='{'){cpbez(buff,1);killsp();}
  150.       if(*s=='{'){
  151.     zlong val; struct Var *v; struct Typ *t;
  152.     val=l2zl(0L);
  153.     s++;killsp();
  154.     while(*s!='}'){
  155.       cpbez(sident,1);killsp();
  156.       if(*sident==0) {error(56);break;}
  157.       t=mymalloc(TYPS);
  158.       t->flags=CONST|INT;
  159.       t->next=0;
  160.       if(find_var(sident,nesting)) error(17,sident);
  161.       v=add_var(sident,t,AUTO,0); /*  AUTO hier klug? */
  162.       if(*s=='='){
  163.         s++;killsp();
  164.         v->clist=initialization(v->vtyp,0,0);
  165.         val=zi2zl(v->clist->val.vint);killsp();
  166.       }else{
  167.         v->clist=mymalloc(CLS);
  168.         v->clist->val.vint=val;
  169.         v->clist->next=v->clist->other=0;
  170.         v->clist->tree=0;
  171.       }
  172.       vlong=l2zl(1L);val=zladd(val,vlong);
  173.       v->vtyp->flags=CONST|ENUM;
  174.       if(*s!='}'&&*s!=',') {error(56);break;}
  175.       if(*s==',') s++;
  176.       killsp();
  177.       if(*s=='}') {s++; break;}
  178.     }
  179.       }
  180.       killsp();
  181.       typ=settyp(INT,typ);*buff=0;
  182.     }
  183.     if(!strcmp("void",buff)) {typ=settyp(VOID,typ);notdone=1;}
  184.     if(!strcmp("char",buff)) {typ=settyp(CHAR,typ);notdone=1;}
  185.     if(!strcmp("short",buff)) {typ=settyp(SHORT,typ);notdone=1;}
  186.     if(!strcmp("int",buff)) {typ=settyp(INT,typ);notdone=1;}
  187.     if(!strcmp("long",buff)) {typ=settyp(LONG,typ);notdone=1;}
  188.     if(!strcmp("float",buff)) {typ=settyp(FLOAT,typ);notdone=1;}
  189.     if(!strcmp("double",buff)) {typ=settyp(DOUBLE,typ);notdone=1;}
  190.     if(!strcmp("const",buff)){
  191.       if(type_qualifiers&CONST) error(58);
  192.       type_qualifiers|=CONST;notdone=1;
  193.     }
  194.     if(!strcmp("volatile",buff)){
  195.       if(type_qualifiers&VOLATILE) error(58);
  196.       type_qualifiers|=VOLATILE;notdone=1;
  197.     }
  198.     if(!strcmp("unsigned",buff)){
  199.       if(type_qualifiers&(XSIGNED|UNSIGNED)) error(58);
  200.       notdone=1;type_qualifiers|=UNSIGNED;
  201.     }
  202.     if(!strcmp("signed",buff)){
  203.       if(type_qualifiers&(XSIGNED|UNSIGNED)) error(58);
  204.       notdone=1;type_qualifiers|=XSIGNED;
  205.     }
  206.     if(!strcmp("auto",buff)) {dsc;storage_class=AUTO;notdone=1;}
  207.     if(!strcmp("register",buff)){dsc;storage_class=REGISTER;notdone=1;}
  208.     if(!strcmp("static",buff)) {dsc;storage_class=STATIC;notdone=1;}
  209.     if(!strcmp("extern",buff)) {dsc;storage_class=EXTERN;notdone=1;}
  210.     if(!strcmp("typedef",buff)) {dsc;storage_class=TYPEDEF;notdone=1;}
  211.     if(!(c_flags[7]&USEDFLAG)&&!strcmp("__reg",buff)){
  212.       char *d;int f=0;
  213.       killsp(); if(*s=='(') s++; else error(151);
  214.       killsp(); if(*s=='\"') s++; else error(74);
  215.       d=buff;
  216.       while(*s&&*s!='\"'){
  217.     if(d-buff-2>MAXI){
  218.       if(!f){ error(206,MAXI);f=1;}
  219.     }else *d++=*s;
  220.     s++;
  221.       }
  222.       *d=0;
  223.       if(*s=='\"') s++; else error(74);
  224.       killsp(); if(*s==')') s++; else error(59);
  225.       for(hard_reg=1;hard_reg<=MAXR;hard_reg++){
  226.     if(!strcmp(buff,regnames[hard_reg])) break;
  227.       }
  228.       if(hard_reg>MAXR){ hard_reg=0;error(220,buff);}
  229.       notdone=1;
  230.     }
  231.     
  232.     if(!notdone&&*buff&&typ==0&&!(type_qualifiers&(XSIGNED|UNSIGNED))){
  233.       v=find_var(buff,0);
  234.       if(v&&v->storage_class==TYPEDEF){
  235.     free(new);
  236.     new=clone_typ(v->vtyp);
  237.     typ=settyp(new->flags,typ);
  238.     notdone=1;
  239.       }
  240.     }
  241.     if(DEBUG&2) printf("typ:%d\n",typ);
  242.   }while(notdone);
  243.   s=merk;killsp();
  244.   return_sc=storage_class;
  245.   return_reg=hard_reg;
  246.   if(typ==0){
  247.     if(storage_class==0&&type_qualifiers==0) {free(new);return(0);}
  248.     typ=INT;
  249.   }
  250.   if(type_qualifiers&(XSIGNED|UNSIGNED))
  251.     if(typ!=INT&&typ!=CHAR&&typ!=LONG&&typ!=SHORT)
  252.       error(58);
  253.   if(DEBUG&2) printf("ts finish:%s\n",s);
  254.   new->flags=typ|type_qualifiers;
  255.   return(new);
  256. }
  257.  
  258. struct Typ *declarator(struct Typ *a)
  259. /* Erzeugt einen neuen Typ, auf Basis des Typs a.           */
  260. /* a wird hiermit verkettet.                                */
  261. {
  262.   struct Typ *t;
  263.   killsp();*ident=0;
  264.   t=direct_declarator(pointer(a));
  265.   if(!a) {if(t) freetyp(t);return(0);} else return(t);
  266. }
  267. struct Typ *pointer(struct Typ *a)
  268. /* Unterroutine fuer declaration(), behandelt Zeiger auf Typ.   */
  269. {
  270.   struct Typ *t;char *merk;int notdone;
  271.   if(!a) return(0);
  272.   killsp();
  273.   while(*s=='*'){
  274.     s++;
  275.     t=mymalloc(TYPS);
  276.     t->flags=POINTER;
  277.     t->next=a;
  278.     a=t;
  279.     do{
  280.       killsp();
  281.       merk=s;cpbez(buff,0);
  282.       notdone=0;
  283.       if(!strcmp("const",buff)) {a->flags|=CONST;notdone=1;}
  284.       if(!strcmp("volatile",buff)) {a->flags|=VOLATILE;notdone=1;}
  285.     }while(notdone);
  286.